﻿#region Using statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Rectangular.Model.Entities;
using Rectangular.Model.Interfaces;
#endregion

namespace Rectangular.Model.Environment
{
    public class Tile
    {
        #region Members
        /* The images used to represent each tile */
        public static new Texture2D Texture;
        /* The ID will identify the type of tile (fe. 0 = water, 1 = rock, ...) */
        private int _id;
        /* The height of the tile */
        private int _height;
        /* The width of the tile */
        private int _width;
        /* The position of the tile */
        private Vector2 _position;
        #endregion

        #region Constructors
        /// <summary>
        /// Constructs a new tile
        /// </summary>
        public Tile()
        {
            _height = 32;
            _width = 32;
        }

        /// <summary>
        /// Constructs a new tile
        /// </summary>
        /// <param name="id">The identifier of the tile</param>
        public Tile(int id) : this()
        {
            _id = id;
        }
        #endregion

        #region Properties
        public int Height
        {
            get { return _height; }
        }

        public int Width
        {
            get { return _width; }
        }

        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Draws the tile
        /// </summary>
        /// <param name="spriteBatch">The sprite batch which is used to draw</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, Position, GetTile(), Color.White);
        }

        /// <summary>
        /// Used to determine collisions
        /// </summary>
        public Rectangle GetBoundingBox()
        {
            return new Rectangle((int)Position.X, (int)Position.Y, Width, Height);
        }

        /// <summary>
        /// Returns a string representation of the object
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return _id.ToString();
        }

        /// <summary>
        /// Returns the appropriate tile image from the atlas
        /// </summary>
        /// <returns>A tile image which represents the current tile</returns>
        private Rectangle GetTile()
        {
            // Get number of horizontal tiles
            int numberOfTilesHorizontal = (int)Texture.Bounds.Width / Width;

            // Get row
            int tileY = (int)(_id / numberOfTilesHorizontal);

            // Get column
            int tileX = _id % numberOfTilesHorizontal;

            // Return result
            return new Rectangle(tileX * Width, tileY * Height, Width, Height);
        }
        #endregion
    }
}